package GraphProperties;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import Global.ConstantVariable;
import Global.DirectoryStructureAndSettingsClass;
import Utility.WorkerUtilityClass;

public class RichClubCoefficientRelatedPropsClass extends GenericGraphPropertyClass {
	
	double[] richClubCoefficientDistrubutionVsDegreeLevels;
	
	RichClubCoefficientRelatedPropsClass(PropertiesOfGraphClass propertiesIBelongTo) {
		super(propertiesIBelongTo);	
	}
	
	public void setRichClubCoefficientVsDegreeDistribution(double[] richClubCoefficientsVsDegreeLevels)
	{
		this.richClubCoefficientDistrubutionVsDegreeLevels = richClubCoefficientsVsDegreeLevels;
		richClubCoefficientCalculationIsCompleted();
	}
	
	void richClubCoefficientCalculationIsCompleted()
	{
		String datasetName = DirectoryStructureAndSettingsClass.getDatasetName();
		
		try {
			
			outForRawFile = createRawRichClubCoefficientResultsFile(datasetName);
			Utility.FileUtilityClass.writeTwoArraysToFile(outForRawFile, propertiesIBelongTo.degreeDistributionRelatedProps.degreeLevels, richClubCoefficientDistrubutionVsDegreeLevels, ConstantVariable.Format_ConstantVariables.numberFormatToBeUsedForDegreesDistributionLevelsOfNodes, ConstantVariable.Format_ConstantVariables.numberFormatToBeUsedForRichClubCoefficientDistributionOfNodes, "RichClub Coefficient Of The Graph for " + datasetName+ConstantVariable.Format_ConstantVariables.DASH_CHAR+propertiesIBelongTo.edgeType);
			outForRawFile.close();
			
			String drawingSetting = null;
			if(propertiesIBelongTo.edgeType==ConstantVariable.Common_ConstantVariables.DEFAULT_EDGE_TYPE)
				drawingSetting = ConstantVariable.Matlab_ConstantVariables.DrawingSettings.RCCRelatedDrawingSettings.DRAWING_SETTING_FOR_RCC_DISTRIBUTION_VS_DEGREE_FIGURE_FOR_LINK_GRAPH;
			else if(propertiesIBelongTo.edgeType==ConstantVariable.Common_ConstantVariables.DEFAULT_EDGE_TYPE_FOR_CONTENT_GRAPH)
				drawingSetting = ConstantVariable.Matlab_ConstantVariables.DrawingSettings.RCCRelatedDrawingSettings.DRAWING_SETTING_FOR_RCC_DISTRIBUTION_VS_DEGREE_FIGURE_FOR_CONTENT_GRAPH;
			
			outForMatlabCodeFile = createMatlabCodeRichClubCoefficientDistributionResultsFile(datasetName);
			createRichClubCoefficientDistributionPlotCodeForMatlab(outForMatlabCodeFile, ConstantVariable.Matlab_ConstantVariables.ScaleType.LINEAR_SCALE, false, ConstantVariable.Matlab_ConstantVariables.GraphOrderType.DONT_CARE, drawingSetting, ConstantVariable.Matlab_ConstantVariables.DistributionType.PDF_TYPE, true);
			outForMatlabCodeFile.close();
			
			outForTexForMatlabFigureFile = createTexFileForTheGeneratedMatlabFigure(propertiesIBelongTo.edgeType, ConstantVariable.Matlab_ConstantVariables.ScaleType.LINEAR_SCALE);
			prepareTexFileForTheCreatedMatlabFigure();			
			outForTexForMatlabFigureFile.close();
			
			copyTexForTheFigureTo(ConstantVariable.DefaultFileNamesConstants.DefaultAllInOneResultsFiles.RCC_VS_DD_MATLAB_FIGURES_TEX_ALL_IN_ONE_FILENAME, false);
						
			/**************************COMBO*********************************/
			
			if(propertiesIBelongTo.edgeType==ConstantVariable.Common_ConstantVariables.DEFAULT_EDGE_TYPE)
			{
				outForMatlabCodeFile = createMatlabCodeRCCDistributionComboResultsFile(datasetName, false);
				createRichClubCoefficientDistributionPlotCodeForMatlab(outForMatlabCodeFile, ConstantVariable.Matlab_ConstantVariables.ScaleType.LINEAR_SCALE, true, ConstantVariable.Matlab_ConstantVariables.GraphOrderType.FIRST_GRAPH, ConstantVariable.Matlab_ConstantVariables.DrawingSettings.RCCRelatedDrawingSettings.DRAWING_SETTING_FOR_RCC_DISTRIBUTION_VS_DEGREE_COMBO_FIGURE_FOR_LINK_GRAPH, ConstantVariable.Matlab_ConstantVariables.DistributionType.PDF_TYPE, true);			
			}
			else if(propertiesIBelongTo.edgeType==ConstantVariable.Common_ConstantVariables.DEFAULT_EDGE_TYPE_FOR_CONTENT_GRAPH)
			{
				outForMatlabCodeFile = createMatlabCodeRCCDistributionComboResultsFile(datasetName, true);
				createRichClubCoefficientDistributionPlotCodeForMatlab(outForMatlabCodeFile, ConstantVariable.Matlab_ConstantVariables.ScaleType.LINEAR_SCALE, true, ConstantVariable.Matlab_ConstantVariables.GraphOrderType.LAST_GRAPH, ConstantVariable.Matlab_ConstantVariables.DrawingSettings.RCCRelatedDrawingSettings.DRAWING_SETTING_FOR_RCC_DISTRIBUTION_VS_DEGREE_COMBO_FIGURE_FOR_CONTENT_GRAPH, ConstantVariable.Matlab_ConstantVariables.DistributionType.PDF_TYPE, true);
			}
				
			outForMatlabCodeFile.close();

			if(propertiesIBelongTo.edgeType==ConstantVariable.Common_ConstantVariables.DEFAULT_EDGE_TYPE_FOR_CONTENT_GRAPH)
			{
				outForTexForMatlabFigureFile = createTexFileForTheGeneratedMatlabFigure(propertiesIBelongTo.edgeType, ConstantVariable.Matlab_ConstantVariables.ScaleType.LINEAR_SCALE);
				prepareTexFileForTheCreatedMatlabFigure();			
				outForTexForMatlabFigureFile.close();
				
				copyTexForTheFigureTo(ConstantVariable.DefaultFileNamesConstants.DefaultAllInOneResultsFiles.RCC_VS_DD_MATLAB_FIGURES_TEX_COMBO_IN_ONE_FILENAME, true);
			}
		} catch (IOException e) {			
			e.printStackTrace();
		}		
	}	
	
    public BufferedWriter createRawPureRichClubCoefficientResultsFile(String datasetName)
    {
    	String path= DirectoryStructureAndSettingsClass.getFullPathOfTheRootDirectoryNameForTheCurrentDataset()
    				+ConstantVariable.DefaultDirectoryNamesConstants.DirectoriesUnderDatasetDirectory.GRAPH_PROPERTIES_FOLDER_NAME
    				+ConstantVariable.DefaultDirectoryNamesConstants.DirectoriesUnderGraphPropertiesDirectory.RICHCLUB_COEFFICIENT_RELATED_DATA_FOLDER_NAME    				
    				+ConstantVariable.DefaultDirectoryNamesConstants.DirectoriesUnderGenericGraphPropertyDirectory.RAW_DATA_FOLDER_NAME;
    	
    	String fileName = datasetName+ConstantVariable.Format_ConstantVariables.DASH_CHAR+propertiesIBelongTo.edgeType+ConstantVariable.Format_ConstantVariables.DASH_CHAR+"RichClubCoefficientsOfNodes.txt"; 
    	try {
    		
    		File descFile=DirectoryStructureAndSettingsClass.createFile(path, fileName);
    		FileWriter fstream;
    		fstream = new FileWriter(descFile);
    		BufferedWriter out = new BufferedWriter(fstream);
    		return out;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
    }
    
    /**********************************************/
	
    public BufferedWriter createRawRichClubCoefficientResultsFile(String datasetName)
    {
    	String path= DirectoryStructureAndSettingsClass.getFullPathOfTheRootDirectoryNameForTheCurrentDataset()
    				+ConstantVariable.DefaultDirectoryNamesConstants.DirectoriesUnderDatasetDirectory.GRAPH_PROPERTIES_FOLDER_NAME
    				+ConstantVariable.DefaultDirectoryNamesConstants.DirectoriesUnderGraphPropertiesDirectory.RICHCLUB_COEFFICIENT_RELATED_DATA_FOLDER_NAME
    				+ConstantVariable.DefaultDirectoryNamesConstants.DirectoriesUnderGenericGraphPropertyDirectory.RAW_DATA_FOLDER_NAME;
    	
    	String fileName = datasetName+ConstantVariable.Format_ConstantVariables.DASH_CHAR+propertiesIBelongTo.edgeType+ConstantVariable.Format_ConstantVariables.DASH_CHAR+"RichClubCoefficientVsDegreeDistribution.txt"; 
    	try {
    		
    		File descFile=DirectoryStructureAndSettingsClass.createFile(path, fileName);
    		FileWriter fstream;
    		fstream = new FileWriter(descFile);
    		BufferedWriter out = new BufferedWriter(fstream);
    		return out;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
    }
    
    /**********************************************/

    public BufferedWriter createMatlabCodeRichClubCoefficientDistributionResultsFile(String datasetName)
    {
    	String path= DirectoryStructureAndSettingsClass.getFullPathOfTheRootDirectoryNameForTheCurrentDataset()
    				+ConstantVariable.DefaultDirectoryNamesConstants.DirectoriesUnderDatasetDirectory.GRAPH_PROPERTIES_FOLDER_NAME
    				+ConstantVariable.DefaultDirectoryNamesConstants.DirectoriesUnderGraphPropertiesDirectory.RICHCLUB_COEFFICIENT_RELATED_DATA_FOLDER_NAME;
    	
    	fullPathOfParentDirectoryOfMatlabCode = path;    	
    	path = path + ConstantVariable.DefaultDirectoryNamesConstants.DirectoriesUnderGenericGraphPropertyDirectory.MATLAB_CODE_FOLDER_NAME;
    	
    	String fileName = datasetName+ConstantVariable.Format_ConstantVariables.DASH_CHAR+propertiesIBelongTo.edgeType+ConstantVariable.Format_ConstantVariables.DASH_CHAR+"RCCDistribution";
    	matlabFileName = fileName;
    	fullPathOfTheMatlabFile = path;
    	
    	fileName += ".m";
    	
    	try {
    		
    		File descFile=DirectoryStructureAndSettingsClass.createFile(path, fileName);
    		FileWriter fstream;
    		fstream = new FileWriter(descFile);
    		BufferedWriter out = new BufferedWriter(fstream);
    		return out;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
    }
    
    /**********************************************/

    public BufferedWriter createMatlabCodeRCCDistributionComboResultsFile(String datasetName, boolean appendMode)
    {
    	String path= DirectoryStructureAndSettingsClass.getFullPathOfTheRootDirectoryNameForTheCurrentDataset()
    				+ConstantVariable.DefaultDirectoryNamesConstants.DirectoriesUnderDatasetDirectory.GRAPH_PROPERTIES_FOLDER_NAME
    				+ConstantVariable.DefaultDirectoryNamesConstants.DirectoriesUnderGraphPropertiesDirectory.RICHCLUB_COEFFICIENT_RELATED_DATA_FOLDER_NAME;
    				    				    	
    	fullPathOfParentDirectoryOfMatlabCode = path;    	
    	path = path + ConstantVariable.DefaultDirectoryNamesConstants.DirectoriesUnderGenericGraphPropertyDirectory.MATLAB_CODE_FOLDER_NAME;
    	
    	String fileName = datasetName+ConstantVariable.Format_ConstantVariables.DASH_CHAR+"RCCCombo";
    	matlabFileName = fileName;
    	fullPathOfTheMatlabFile = path;
    	
    	fileName += ".m";
    	
    	try {
    		
    		File descFile=DirectoryStructureAndSettingsClass.createFile(path, fileName);
    		FileWriter fstream;
    		fstream = new FileWriter(descFile, appendMode);
    		BufferedWriter out = new BufferedWriter(fstream);
    		return out;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
    }
    
    void createRichClubCoefficientDistributionPlotCodeForMatlab(BufferedWriter outForMatlabCodeFile, int type, boolean isForCombo, int order, String drawingSettings, int distributionPlotType, boolean generateFigure)
    {    	
    	double[] degreeLevels = WorkerUtilityClass.invertConvertionOfSomeScale(propertiesIBelongTo.degreeDistributionRelatedProps.degreeLevels);
    	createPlotCodeForMatlab(outForMatlabCodeFile, type, isForCombo, order, degreeLevels, richClubCoefficientDistrubutionVsDegreeLevels, null, "RichClub Coefficient vs Degree Distribution", "Degree (D)", "RichClub Coefficient (RCC)", drawingSettings, distributionPlotType, generateFigure);
    }
}
